package com.canbot.u05.activity.customvoice.tool

import com.canbot.u05.activity.*

import com.canbot.u05.activity.customvoice.bean.CustomDialogExcelData

import java.util.ArrayList

import jxl.Workbook
import jxl.WorkbookSettings
import jxl.format.BorderLineStyle
import jxl.format.Colour
import jxl.write.WritableFont
import jxl.write.*
import jxl.format.Alignment
import jxl.format.Border

/**
 * ExportExcelUtils
 *
 * @author Liu.Heng
 * @date 2019/6/5
 */
object ExportExcelUtils {

    private lateinit var sWritableCellFormat14: WritableCellFormat

    private lateinit var sWritableCellFormat10: WritableCellFormat

    private lateinit var sWritableCellFormat12: WritableCellFormat

    private const val UTF8_ENCODING = "UTF-8"

    /**
     * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
     */
    private fun format() {
        try {
            val sWritableFont14 = WritableFont(WritableFont.ARIAL, 14)
            sWritableFont14.colour = Colour.LIGHT_BLUE
            sWritableCellFormat14 = WritableCellFormat(sWritableFont14)
            sWritableCellFormat14.alignment = Alignment.CENTRE
            sWritableCellFormat14.setBorder(Border.ALL, BorderLineStyle.THIN)
            sWritableCellFormat14.setBackground(Colour.VERY_LIGHT_YELLOW)

            val sWritableFont10 = WritableFont(WritableFont.ARIAL, 10)
            sWritableCellFormat10 = WritableCellFormat(sWritableFont10)
            sWritableCellFormat10.alignment = Alignment.CENTRE
            sWritableCellFormat10.setBorder(Border.ALL, BorderLineStyle.THIN)
            sWritableCellFormat10.setBackground(Colour.GRAY_25)

            val sWritableFont12 = WritableFont(WritableFont.ARIAL, 10)
            sWritableCellFormat12 = WritableCellFormat(sWritableFont12)
            /**对齐格式*/
            sWritableCellFormat10.alignment = Alignment.CENTRE
            /**设置边框*/
            sWritableCellFormat12.setBorder(Border.ALL, BorderLineStyle.THIN)

        } catch (e: WriteException) {
            e.printStackTrace()
        }

    }

    /**
     * 初始化Excel
     *
     * @param fileName 导出excel存放的地址（目录）
     * @param colName  excel中包含的列名（可以有多个）
     */
    fun initExcel(fileName: String, colName: Array<String>) {
        format()
        var workbook: WritableWorkbook? = null
        try {
            val file = fileName.file()
            if (!file.exists()) file.createNewFile()
            loge("$file")
            workbook = Workbook.createWorkbook(file)
            /**设置表格的名字*/
            val sheet = workbook.createSheet("自定义对话数据", 0)
            /**创建标题栏*/
            sheet.addCell(Label(0, 0, fileName, sWritableCellFormat14))

            colName.indices.forEach {
                sheet.addCell(Label(it, 0, colName[it], sWritableCellFormat10))
            }
            /**设置行高*/
            sheet.setRowView(0, 340)
            workbook.write()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (workbook != null) {
                try {
                    workbook.close()
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
        }
    }

    fun <T> writeObjListToExcel(objList: List<T>?, fileName: String) {

        var writeBook: WritableWorkbook? = null

        if (objList.isNotEmpty2()) {

            try {
                val setEncode = WorkbookSettings()
                setEncode.encoding = UTF8_ENCODING

                fileName.fileInputStream().use { obj ->

                    val workbook = Workbook.getWorkbook(obj)
                    writeBook = Workbook.createWorkbook(fileName.file(), workbook)
                    val sheet = writeBook?.getSheet(0)
                    objList.withIndex().forEach {

                        val (_, question_str, image_path, _, tts_text) = it.value as CustomDialogExcelData
                        val list = ArrayList<String>()
                        list.add(question_str)
                        list.add(tts_text)
                        list.add(image_path)

                        list.withIndex().forEach { i ->
                            sheet?.addCell(Label(i.index, it.index + 1, i.value, sWritableCellFormat12))
                            /**设置列宽*/
                            sheet?.setColumnView(i.index, i.value.length + 300)
                        }
                        /**设置行高*/
                        sheet?.setRowView(it.index + 1, 350)
                    }

                    writeBook?.write()
                }

            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                if (writeBook != null) {
                    try {
                        writeBook?.close()
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                }
            }
        }
    }
}
